package net.hctek.AnnouncerStation.util;

import java.io.IOException;  
import java.io.PrintWriter;  
import com.alibaba.fastjson.*;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.http.HttpServletResponse;  
 

public class ResponseJsonUtils {
 
    private static String encoding = "UTF-8";  
      

    private static String callback = "callback";  
      

    private static SerializerFeature[] features =  new SerializerFeature[]{  
        
        SerializerFeature.WriteMapNullValue,  
          
       
        SerializerFeature.WriteNullBooleanAsFalse,  
          
      
        SerializerFeature.WriteNullListAsEmpty,  
          
        
        SerializerFeature.WriteNullNumberAsZero,  
          
        
        SerializerFeature.WriteNullStringAsEmpty,  
          
        
        SerializerFeature.WriteDateUseDateFormat  
    };  
      

    private static String toJSONString(Object obj){  
        return JSON.toJSONString(obj, features);  
    }  
      

    public static void json(HttpServletResponse response, Object data, String encoding){  
        
        response.setContentType("text/plain;charset=" + encoding);  
        response.setCharacterEncoding(encoding);  
          
        PrintWriter out = null;  
        try{  
            out = response.getWriter();  
            out.write(toJSONString(data));  
            out.flush();  
        }catch(IOException e){  
            e.printStackTrace();  
        }  
    }  
      

    public static void json(HttpServletResponse response, Object data){  
        json(response, data, encoding);  
    }  
      
  
    public static void jsonp(HttpServletResponse response, Object data){  
        jsonp(response, callback, data, encoding);  
    }  
      

    public static void jsonp(HttpServletResponse response, String callback, Object data){  
        jsonp(response, callback, data, encoding);  
    }  
      
    
    public static void jsonp(HttpServletResponse response, String callback, Object data, String encoding){  
        StringBuffer sb = new StringBuffer(callback);  
        sb.append("(");  
        sb.append(toJSONString(data));  
        sb.append(");");  
  
        response.setContentType("text/plain;charset=" + encoding);  
        response.setCharacterEncoding(encoding);  
  
        PrintWriter out = null;  
        try {  
            out = response.getWriter();  
            out.write(sb.toString());  
            out.flush();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public static String getEncoding() {  
        return encoding;  
    }  
  
    public static void setEncoding(String encoding) {  
        ResponseJsonUtils.encoding = encoding;  
    }  
  
    public static String getCallback() {  
        return callback;  
    }  
  
    public static void setCallback(String callback) {  
        ResponseJsonUtils.callback = callback;  
    }  
}
